﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace WebChatServer.Models
{
    public class GroupRepository : IGroupRepository
    {
        AccountEntities _entities = new AccountEntities();

        public FriendGroup GetGroup(string gGroupId)
        {
            var lstGroup = from grp in _entities.Groups
                           where grp.IDGroup == gGroupId
                           select grp;

            if (lstGroup != null && lstGroup.Count() > 0)
            {
                Group group = lstGroup.ToArray()[0];
                return new FriendGroup(){GroupId = gGroupId, UserName = group.IDMember, Name = group.Name, Friends = null};
            }

            return null;
        }

        public Group[] Check(string memberId)
        {
            var lstGroup = from grp in _entities.Groups
                           where grp.IDMember == memberId
                         select grp;

            if (lstGroup != null && lstGroup.Count() > 0)
            {
                return lstGroup.ToArray();
            }

            return null;
        }

        public bool isExist(string gGroupId)
        {
            var lstGroup = from grp in _entities.Groups
                           where grp.IDGroup == gGroupId
                           select grp;

            if (lstGroup != null && lstGroup.Count() > 0)
            {
                return true;
            }

            return false;
        }

        public bool isExist(FriendGroup gGroup)
        {
            var lstGroup = from grp in _entities.Groups
                         where grp.IDGroup == gGroup.GroupId && grp.IDMember == gGroup.UserName
                         select grp;

            if (lstGroup != null && lstGroup.Count() > 0)
            {
                return true;
            }

            return false;
        }

        public FriendGroup AddGroup(FriendGroup gGroup)
        {
            if (isExist(gGroup))
                return null;

            Group group = new Group();
            group.IDMember = gGroup.UserName;
            group.IDGroup = gGroup.UserName + "_" + gGroup.Name;
            group.Name = gGroup.Name;
            group.IsExist = true;

            _entities.Groups.AddObject(group);
            _entities.SaveChanges();

            return gGroup;
        }

        public bool UpdateGroup(Member member, string oldGroupName, string newGroupName)
        {
            var lstGroup = from grp in _entities.Groups
                           where grp.IDGroup == member.Id + "_" + oldGroupName
                           select grp;

            if (lstGroup != null && lstGroup.Count() > 0)
            {
                Group group = _entities.Groups.Single(u => u.IDGroup == member.Id + "_" + oldGroupName);
                group.Name = newGroupName;
                _entities.SaveChanges();
                return true;
            }

            return false;
        }

        public bool DeleteFriendsInGroup(string groupId)
        {
            var lstMem = from mem in _entities.FriendShips
                         where mem.IDGroup == groupId
                         select mem;

            if (lstMem != null && lstMem.Count() > 0)
            {
                FriendShip[] friendShip = lstMem.ToArray();
                Member[] friends = new Member[friendShip.Count()];
                for (int i = 0; i < friendShip.Count(); i++)
                {
                    _entities.FriendShips.DeleteObject(friendShip[i]);
                    _entities.SaveChanges();
                }

                return true;
            }

            return false;
        }

        public bool DeleteGroup(string gGroupId)
        {
            var lstGroup = from grp in _entities.Groups
                           where grp.IDGroup == gGroupId
                           select grp;

            if (lstGroup != null && lstGroup.Count() > 0)
            {
                Group gGroup = _entities.Groups.Single(u => u.IDGroup == gGroupId);
                DeleteFriendsInGroup(gGroupId);

                _entities.Groups.DeleteObject(gGroup);
                _entities.SaveChanges();
                return true;
            }

            return false;
        }
    }
}